﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.EnterpriseLibrary.Caching;

namespace PJ.Data
{
    /// <summary>
    /// ObjectPICProvider : Persist In Cache data provider
    /// </summary>
    /// <typeparam name="I">Object to provide interface type</typeparam>
    /// <typeparam name="C">Object to provide concrete class type</typeparam>
    public abstract class ItemPICProvider<I, C> : EntityPICProvider<I, C>, IItemProvider<I>
        where C : BaseItemData, I, new()
        where I : class, IItemData
    {
        public ItemPICProvider(ICacheManager cache) : base(cache)
        {
        }
        public I GetByName(string name)
        {
            if (string.IsNullOrEmpty(name))
                throw new ArgumentNullException();
            return DataObjects.Where(p=>p.Name == name).Single();
        }

        public I CachedGetByName(string name)
        {
            return GetByName(name);
        }

        public I FindByName(string name)
        {
            return DataObjects.Where(p=>p.Name == name).SingleOrDefault();
        }

        public I CachedFindByName(string name)
        {
            return FindByName(name);
        }

        public IEnumerable<string> Search(string searchTxt)
        {
            return (from p in DataObjects where p.Name.Contains(searchTxt) select p.GlobalId);
        }

        public IEnumerable<string> CachedSearch(string searchTxt)
        {
            return Search(searchTxt);
        }

        #region Interface override
        IItemData IItemProvider.GetByName(string name)
        {
            return GetByName(name);
        }

        IItemData IItemProvider.CachedGetByName(string name)
        {
            return CachedGetByName(name);
        }


        IItemData IItemProvider.FindByName(string name)
        {
            return FindByName(name);
        }

        IItemData IItemProvider.CachedFindByName(string name)
        {
            return CachedFindByName(name);
        }


        #endregion

    }
}
